using System;
using System.Collections.Generic;
using System.Text;	 
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace Possan.Graphics.Filters
{						  
	public class OldResizeFilter : IBaseFilter
	{
		public string ParameterDefinition
		{
			get
			{
				     return null;
			}
		}

		private int _ow;

		public int OutputWidth
		{
			get {      return _ow; }
			set { _ow = value; }
		}

		private int _oh;

		public int OutputHeight
		{
			get {      return _oh; }
			set { _oh = value; }
		}

		private int _ax;

		public int AlignX
		{
			get {      return _ax; }
			set { _ax = value; }
		}

		private int _ay;

		public int AlignY
		{
			get {      return _ay; }
			set { _ay = value; }
		}

		private string _wmf;

		public string WatermarkFile
		{
			get {      return _wmf; }
			set { _wmf = value; }
		}

		private bool stf;

		public bool ScaleToFit
		{
			get {      return stf; }
			set { stf = value; }
		}


		bool _AllowUpscaling;

		public bool AllowUpscaling
		{
			get {      return _AllowUpscaling; }
			set { _AllowUpscaling = value; }
		}

	//	bool _AllowDownscaling;



		public OldResizeFilter()
		{
			ScaleToFit = false;
			OutputWidth = 0;
			OutputHeight = 0;
			WatermarkFile = "";
			AlignX = 50;
			AlignY = 50;
			AllowUpscaling = true;
		}
						 
		public Bitmap Filter(Bitmap inputbitmap)
		{
			int outputwidth = OutputWidth;
			int outputheight = OutputHeight;

#if DEBUG									  
	//		Logger.Warning("Utilities::MakeThumbnail( input=" + inputbitmap.Width + "x" + inputbitmap.Height + " output=" + outputwidth + "x" + outputheight + " )");
#endif
						  
			float oldaspect = (float)inputbitmap.Width / (float)inputbitmap.Height;

			// System.Web.HttpContext.Current.Response.Write( "oldaspect = "+oldaspect.ToString()+"<br>" );

			if (outputwidth == 0)
			{
				if (outputheight != 0)
				{
					// height = oldimg.Height 
					outputwidth = (int)(outputheight * oldaspect);
					
					if (!AllowUpscaling)
						outputwidth = (int)Math.Min(inputbitmap.Width, outputwidth);
				}
				else
				{
					outputwidth = inputbitmap.Width;
				}
			}

			if (outputheight == 0)
			{
				if (outputwidth != 0)
				{
					// width = oldimg.Width;
					outputheight = (int)(outputwidth / oldaspect);
					
					if (!AllowUpscaling)
						outputheight = (int)Math.Min(inputbitmap.Height, outputheight);
				}
				else
				{
					outputheight = inputbitmap.Height;
				}
			}




			Bitmap outputbitmap = new Bitmap(outputwidth, outputheight, PixelFormat.Format32bppArgb);
			outputbitmap.SetResolution(inputbitmap.HorizontalResolution, inputbitmap.VerticalResolution);

			System.Drawing.Graphics outputgfx = System.Drawing.Graphics.FromImage(outputbitmap);
			outputgfx.FillRectangle(new SolidBrush(Color.Transparent), 0, 0, outputwidth, outputheight);
			// g.DrawImage(inputbitmap, 0, 0);

			//
			// Passa in bilden p� b�sta s�tt... �r originalbilden mindre �n destinationsbilden, skala INTE upp bilden
			//

			int thumbwidth = inputbitmap.Width;
			int thumbheight = inputbitmap.Height;

			float dx = (float)outputwidth / (float)inputbitmap.Width;
			float dy = (float)outputheight / (float)inputbitmap.Height;

			float mult = Math.Max(dx, dy);
			if (ScaleToFit  )
				mult = Math.Min(dx, dy);

			if( !AllowUpscaling  )
				if( mult > 1.0f   )
					mult = 1.0f;

			//	System.Web.HttpContext.Current.Response.Write("mult="+ mult+ "<br>");

			thumbwidth = (int)(inputbitmap.Width * mult);
			thumbheight = (int)(inputbitmap.Height * mult);
		
			int maxoffsetx = outputwidth - thumbwidth;
			int maxoffsety = outputheight - thumbheight;
#if DEBUG
			Logger.Debug(this.GetType(), "maxoffset=" + maxoffsetx + ", " + maxoffsety);
#endif
			int thumbx = (maxoffsetx * AlignX) / 100;
			int thumby = (maxoffsety * AlignY) / 100;
#if DEBUG
			Logger.Debug(this.GetType(), "thumb @ " + thumbx + ", " + thumby);
#endif
			//
			// Skala om bilden
			//

			outputgfx.InterpolationMode =  InterpolationMode.HighQualityBicubic;
			outputgfx.DrawImage(inputbitmap, thumbx, thumby, thumbwidth, thumbheight);


			/*	if (watermark != null && (outputwidth > 50 || outputheight > 50))
			{

				// int watermarkorigwidth = watermarkimg.Width;
				// int watermarkorigheight = watermarkimg.Height;
				float watermarkorigaspect = (float)watermark.Height / (float)watermark.Width;

				int watermarkwidth = thumbwidth;
				int watermarkheight = (int)(thumbwidth * watermarkorigaspect);
				int watermarkx = thumbx;
				int watermarky = thumby + thumbheight - watermarkheight;

				watermarkx = Math.Min(watermarkx, (outputwidth - watermarkwidth));
				watermarky = Math.Min(watermarky, (outputheight - watermarkheight));

				outputgfx.DrawImage(watermark, watermarkx, watermarky, watermarkwidth, watermarkheight);

			}	*/

			return outputbitmap;

		}		   
	}
}





